package com.miyu.system.service.user.impl;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.plugins.utils.Comutils;
import com.miyu.system.dao.user.SysUserDao;
import com.miyu.system.pojo.system.SysUser;
import com.miyu.system.service.user.SysUserService;
import com.miyu.system.utils.enumeration.LoginEnum;
import com.miyu.system.utils.enumeration.R;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统用户服务impl
 * @author miyu
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class SysUserServiceImpl implements SysUserService {

    private final SysUserDao dao;


    /**
     * 系统用户编辑
     * @param user 用户
     * @return {@link SysUser}
     */
    @Override
    public SysUser sysUserEdit(SysUser user) {

        String userId = Comutils.getUUID();
        // if(StringUtils.isBlank(user.getUserId())){
        //     user.setUserId(userId);
        // }

        user = dao.saveOrUpdateByNotNullProperties(user);
        return user;
    }

    /**
     * 查询系统用户
     * @param params 参数个数
     * @return {@link JSONArray}
     */
    @Override
    public Page<SysUser> querySysUser(JSONObject params) {
        return dao.querySysUser(params);
    }

    /**
     * ▽系统用户
     * @param sysUserList 系统用户列表
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject delSysUser(List<SysUser> sysUserList) {
        dao.deleteAllInBatch(sysUserList);
        return R.result("101");
    }

    /**
     * 用户登录
     * @param params 参数个数
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject userLogin(JSONObject params) {
        System.out.println(params);
        /*后期优化*/
        String account = params.getString("userAccount");
        String password = params.getString("password");
        /*登录参数添加*/
        SaLoginModel saLoginModel = new SaLoginModel();
        saLoginModel.setDevice("PC");
        saLoginModel.setTimeout(100);
        StpUtil.login(account,saLoginModel);
        System.out.println(StpUtil.getAnonTokenSession());

        return LoginEnum.getInfoByCode("1001");
    }


    /**
     * 用户退出
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject loginOut() {
        System.out.println(StpUtil.isLogin());
        System.out.println(StpUtil.getLoginId());
        if (!StpUtil.isLogin()) {
            return LoginEnum.getInfoByCode("1006");
        }else{
            StpUtil.logout();
        }
        return LoginEnum.getInfoByCode("1007");

    }
    /**
     * 校验码
     * @param ipAddr ip addr
     * @param code   代码
     * @return {@link String}
     */
    @Override
    public void checkCode(String ipAddr, String code) {
        /* 缓存验证码 设置5分钟过期*/
//        RedisUtils.set("system:loginCode:"+ipAddr,code,300);

    }

}
